package com.bdqn.qnnews.search.controller.v1;

import com.bdqn.qnnews.articel.api.dto.ApArticleDto;
import com.bdqn.qnnews.common.base.PageResult;
import com.bdqn.qnnews.common.base.R;
import com.bdqn.qnnews.search.convert.AppInfoArticleConvert;
import com.bdqn.qnnews.search.entity.AppInfoArticle;
import com.bdqn.qnnews.search.service.ApArticleSearchService;
import com.bdqn.qnnews.search.vo.UserSearchReqVo;
import com.bdqn.qnnews.search.vo.UserSearchRespVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RequiredArgsConstructor
@Tag(name = "文章搜索API",description = "文章搜索API")
@RestController
@RequestMapping("/api/v1/article")
public class ArticleSearchController{

    private final ApArticleSearchService searchService;

    @Operation(summary = "文章搜索")
    @PostMapping("/search/search")
    public PageResult<ArrayList<UserSearchRespVo>> search(@RequestBody UserSearchReqVo userSearchReqVo) {
        // 文章搜索接口
        return searchService.search(userSearchReqVo);
    }


    @Operation(summary = "添加文章索引文档")
    @PostMapping("/doc")
    public R<String> addArticleDoc(@RequestBody ApArticleDto articleDto){
        // 转换
        final AppInfoArticle appInfoArticle = AppInfoArticleConvert.INSTANCE.dtoToEntity(articleDto);
        searchService.saveAndUpdateArticle(appInfoArticle);
        return R.ok("添加或更新索引文档成功");
    }

    @Operation(summary = "删除文章索引文档")
    @DeleteMapping("/doc/{articleId}")
    public R<String> delArticleDoc(@PathVariable String articleId){
        searchService.deleteArticle(articleId);
        return R.ok("删除索引文档成功");
    }
}